﻿using MES.Desktop.Entities;
using MES.Desktop.IDataAccess;
using MES.Desktop.IService;
using System.ComponentModel;
using System.Data;
using System.IO;

namespace MES.Desktop.Service
{
    public class FileService : ServiceBase, IFileService
    {
        IFileAccess _fileAccess;
        public FileService(IFileAccess fileAccess)
        {
            _fileAccess = fileAccess;
        }

        public int DeleteFile(string fileName)
        {
            string json = _fileAccess.DeleteFile(fileName);
            return this.GetResult<int>(json);
        }

        public FileEntiy[] GetLocalUpgradeFiles()
        {
            var files = _fileAccess.GetLocalUpgradeFiles();

            return files.AsEnumerable().Select(row => new FileEntiy
            {
                fileName = row["file_name"].ToString(),
                fileMd5 = row["file_md5"].ToString()
            }).ToArray();
        }

        public FileEntiy[] GetServerUpgradeFiles(string key = "")
        {
            string json = _fileAccess.GetServerUpgradeFiles(key);
            return this.GetResult<FileEntiy[]>(json);
        }

        public void UploadFile(string file, string filePath,
            Action<int> prograssChanged,
            Action<AsyncCompletedEventArgs> completed)
        {
            _fileAccess.UploadFile(file, filePath, prograssChanged, completed);
        }

        public void UploadIdCard(string file, string name)
        {
            _fileAccess.UploadIdCard(file, name);
        }

        public void UploadIssueImage(string file, string name)
        {
            _fileAccess.UploadIssueImage(file, name);
        }
    }
}
